/**
 * 
 */
package com.example.radioclub.utilities;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.Header;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

import com.example.radioclub.data.Constants;

import android.util.Base64;
import android.util.Log;

/**
 * This class in for tools of Melon API which is to construct requests based on
 * each API. This class contains static methods to make HTTP POST and GET
 * request.
 * 
 * @author wizard
 * 
 */
public class MelonApiHttpRequestTools {
	private static final String TAG = Constants.APP_TAG + "Melon API Tools";

	/**
	 * Return a response of Http Post request.
	 * 
	 * @param httpPostRequest
	 *            request you want to submit.
	 * @return response or <b><i>null</i></b> if something goes wrong.
	 */
	public static HttpResponse getHTTPPostResponse(HttpPost httpPostRequest) {
		HttpResponse response = null;
		if (httpPostRequest == null)
			return null;
		else if (httpPostRequest != null) {
			try {
				HttpClient httpClient = new DefaultHttpClient();
				response = httpClient.execute(httpPostRequest);
			} catch (ClientProtocolException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				Log.i(TAG, e.getMessage());
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				Log.i(TAG, e.getMessage());
			}
		}

		if (response == null) {
			Log.i(TAG, "Response is null");
		} else {
			Log.i(TAG, "Response is not null");
		}

		return response;
	}

	/**
	 * Returns a response from Http Get request.
	 * 
	 * @param httpGetRequest
	 *            request you want to submit.
	 * @return response or <b><i>null</i></b> if something goes wrong.
	 */
	public static HttpResponse getHTTPGetResponse(HttpGet httpGetRequest) {
		HttpResponse response = null;
		if (httpGetRequest == null)
			return null;
		else if (httpGetRequest != null) {
			try {
				HttpClient httpClient = new DefaultHttpClient();
				response = httpClient.execute(httpGetRequest);
			} catch (ClientProtocolException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				Log.i(TAG, e.getMessage());
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				Log.i(TAG, e.getMessage());
			}
		}

		return response;
	}

	/**
	 * Creates a HTTP POST request with provided variables to log in with
	 * client.
	 * 
	 * @param clientName
	 *            the name of client, can't be <b>null</b>.
	 * @param password
	 *            password this client uses, can't be <b>null</b>.
	 * @return a HTTP POST request with certain variables or <b><i>null</i></b>
	 *         if either clientName or password or both are null.
	 */
	public static HttpPost getClientAuthenticationRequest(String clientName,
			String password) {
		if (clientName == null || password == null || password.length() <= 0
				|| clientName.length() <= 0)
			return null;
		HttpPost request = new HttpPost(Constants.API_MELON_AUTH_CLIENT);
		try {
			request.addHeader("Accept", "application/json");

			List<BasicNameValuePair> valuePairs = new ArrayList<BasicNameValuePair>();
			valuePairs.add(new BasicNameValuePair("clientName", clientName));
			valuePairs.add(new BasicNameValuePair("password", password));

			request.setEntity(new UrlEncodedFormEntity(valuePairs));

		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			Log.i(TAG, e.getMessage());
			e.printStackTrace();
		}

		return request;
	}

	/**
	 * Creates a HTTP POST request with provided variables for logging in as a
	 * USER <b><i>NOT</i></b> CLIENT.
	 * 
	 * @param userEmail
	 *            email user signed up with, can't be <b>null</b>.
	 * @param password
	 *            user's password, can't be <b>null</b>.
	 * @return a HTTP POST request with certain variables or <b><i>null</i></b>
	 *         if either userEmail or password or both are <b>null</b>.
	 */
	public static HttpPost getUserAuthenticationRequest(String userEmail,
			String password) {
		if (userEmail == null || password == null || userEmail.length() <= 0
				|| password.length() <= 0)
			return null;

		HttpPost request = new HttpPost(Constants.API_MELON_AUTH_USER);
		String encodedPassword = Base64.encodeToString(password.getBytes(),
				Base64.DEFAULT);
		try {
			request.addHeader("Accept", "application/json");

			List<BasicNameValuePair> valuePairs = new ArrayList<BasicNameValuePair>();
			valuePairs.add(new BasicNameValuePair("userEmail", userEmail));
			valuePairs.add(new BasicNameValuePair("password", encodedPassword));

			request.setEntity(new UrlEncodedFormEntity(valuePairs));
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			Log.i(TAG, e.getMessage());
			e.printStackTrace();
		}
		return request;
	}

	/**
	 * Creates a HTTP GET request to get (obviously) a daily chart. This request
	 * doesn't need any authentication first.
	 * 
	 * @param offset
	 *            starting point of the chart, default value is 0
	 * @param limit
	 *            the sum of chart you will get and can be <b>null</b> which
	 *            means you will get everything for this chart. default value is
	 *            30.
	 * @param headers
	 *            headers you might want to attach.
	 * @return an object of this request or <b><i>null</i></b>
	 */
	public static HttpGet getDailyChartRequest(int offset, int limit,
			Header[] headers) {
		if (offset < 0)
			offset = 0;
		if (limit <= 0)
			limit = 30;

		List<BasicNameValuePair> valuePairs = new ArrayList<BasicNameValuePair>();
		valuePairs.add(new BasicNameValuePair("offset", offset + ""));
		valuePairs.add(new BasicNameValuePair("limit", limit + ""));

		String url = URLEncodedUtils.format(valuePairs, "utf-8");
		url = Constants.API_MELON_CHART_DAILY + "?" + url;
		Log.i(TAG, url);

		HttpGet request = new HttpGet(url);
		request.addHeader("Accept", "application/json");
		if (headers != null) {
			for (int i = 0; i < headers.length; ++i) {
				request.addHeader(headers[i]);
			}
		}

		return request;
	}

	/**
	 * Creates a HTTP GET request to get weekly chart. This request needs
	 * authentication first.
	 * 
	 * @param headers
	 *            headers you might want to attach first.
	 * @return an object of this request.
	 */
	public static HttpGet getWeeklyChartRequest(Header[] headers) {
		HttpGet request = new HttpGet(Constants.API_MELON_CHART_WEEKLY);
		request.addHeader("Accept", "application/json");
		if (headers != null) {
			for (int i = 0; i < headers.length; ++i) {
				request.addHeader(headers[i]);
			}
		}
		return request;
	}

	/**
	 * Creates a HTTP GET request to get weekly chart starting from certain date
	 * you provide.
	 * 
	 * @param offset
	 *            the starting point of the list.
	 * @param limit
	 *            the sum of data appeared on the chart. default value is 30.
	 * @param date
	 *            the date your list will start from in two digits, can't be
	 *            <b>null</b>.
	 * @param month
	 *            the month in two digits, can't be <b>null</b>.
	 * @param year
	 *            the year in two digits, can't be <b>null</b>.
	 * @return an object of this request or <b><i>null</i></b> if you entered
	 *         invalid date.
	 */
	public static HttpGet getWeeklyChartAtDateRequest(int offset, int limit,
			int date, int month, int year, Header[] headers) {
		if (limit <= 0)
			limit = 30;

		if (date <= 0 || month <= 0 || year <= 0)
			return null;
		String sdate = date + "", smonth = month + "", syear = year + "";
		if (sdate.length() < 2)
			sdate = "0" + sdate;
		if (smonth.length() < 2)
			smonth = "0" + smonth;
		if (syear.length() < 1000)
			return null;
		String startDate = syear + smonth + sdate;

		List<BasicNameValuePair> valuePairs = new ArrayList<BasicNameValuePair>();
		valuePairs.add(new BasicNameValuePair("offset", offset + ""));
		valuePairs.add(new BasicNameValuePair("limit", limit + ""));
		valuePairs.add(new BasicNameValuePair("startDate", startDate));

		String url = URLEncodedUtils.format(valuePairs, "utf-8");
		url = Constants.API_MELON_CHART_WEEKLY_STARTDATE + url;
		Log.i(TAG, url);

		HttpGet request = new HttpGet(url);
		request.addHeader("Accept", "application/json");
		if (headers != null) {
			for (int i = 0; i < headers.length; ++i) {
				request.addHeader(headers[i]);
			}
		}

		return request;
	}

	/**
	 * Creates a HTTP GET request for getting weekly latest chart.
	 * 
	 * @param offset
	 *            the starting point of the list for the chart.
	 * @param limit
	 *            the sum of data you want to be on the list.
	 * @return an object of this request
	 */
	public static HttpGet getWeeklyLatestChartRequest(int offset, int limit,
			Header[] headers) {

		List<BasicNameValuePair> valuePairs = new ArrayList<BasicNameValuePair>();
		valuePairs.add(new BasicNameValuePair("offset", offset + ""));
		valuePairs.add(new BasicNameValuePair("limit", limit + ""));

		String url = URLEncodedUtils.format(valuePairs, "utf-8");
		url = Constants.API_MELON_CHART_WEEKLY_LATEST + url;
		Log.i(TAG, url);

		HttpGet request = new HttpGet(url);
		request.addHeader("Accept", "application/json");
		if (headers != null) {
			for (int i = 0; i < headers.length; ++i) {
				request.addHeader(headers[i]);
			}
		}

		return request;
	}

	/**
	 * Creates HTTP GET request to get monthly chart. This request needs
	 * authentication first.
	 * 
	 * @return object of the request
	 */
	public static HttpGet getMonthlyChartRequest(Header[] headers) {
		HttpGet request = new HttpGet(Constants.API_MELON_CHART_MONTHLY);
		request.addHeader("Accept", "application/json");
		if (headers != null) {
			for (int i = 0; i < headers.length; ++i) {
				request.addHeader(headers[i]);
			}
		}
		return request;
	}

	/**
	 * 
	 * @param offset
	 *            the starting point of the list, if offset < 0 then it set to
	 *            0.
	 * @param limit
	 *            the number of resulting records from the list. default is 30;
	 * @param date
	 *            the date of the chart, can't be <b>null</b>.
	 * @param month
	 *            the month, can't be <b>null</b>.
	 * @param year
	 *            the year, can't be <b>null</b>.
	 * @return object of the request.
	 */
	public static HttpGet getMonthlyChartAtDateRequest(int offset, int limit,
			int date, int month, int year, Header[] headers) {
		if (offset < 0)
			offset = 0;
		if (limit <= 0)
			limit = 30;
		if (date <= 0 || month <= 0 || year <= 0)
			return null;
		String sdate = date + "", smonth = month + "", syear = year + "";
		if (sdate.length() < 2)
			sdate = "0" + sdate;
		if (smonth.length() < 2)
			smonth = "0" + smonth;
		if (syear.length() < 4)
			return null;
		String startDate = syear + smonth + sdate;

		List<BasicNameValuePair> valuePairs = new ArrayList<BasicNameValuePair>();
		valuePairs.add(new BasicNameValuePair("offset", offset + ""));
		valuePairs.add(new BasicNameValuePair("limit", limit + ""));
		valuePairs.add(new BasicNameValuePair("startDate", startDate));

		String url = URLEncodedUtils.format(valuePairs, "utf-8");
		url = Constants.API_MELON_CHART_MONTHLY + url;

		HttpGet request = new HttpGet(url);
		request.addHeader("Accept", "application/json");
		if (headers != null) {
			for (int i = 0; i < headers.length; ++i) {
				request.addHeader(headers[i]);
			}
		}
		return request;
	}

	/**
	 * Creates the object of the request for monthly chart based on some
	 * parameters you provided.
	 * 
	 * @param offset
	 *            the starting point of the list with dafault value is 0.
	 * @param limit
	 *            the number of record you choose to display with default value
	 *            is 30.
	 * @return object of the request itself
	 */
	public static HttpGet getMonthlyLatestChartRequest(int offset, int limit,
			Header[] headers) {
		if (offset < 0)
			offset = 0;
		if (limit <= 0)
			limit = 30;

		List<BasicNameValuePair> valuePairs = new ArrayList<BasicNameValuePair>();
		valuePairs.add(new BasicNameValuePair("offset", offset + ""));
		valuePairs.add(new BasicNameValuePair("limit", limit + ""));

		String url = URLEncodedUtils.format(valuePairs, "utf-8");
		url = Constants.API_MELON_CHART_WEEKLY_LATEST + url;

		HttpGet request = new HttpGet(url);
		request.addHeader("Accept", "application/json");
		if (headers != null) {
			for (int i = 0; i < headers.length; ++i) {
				request.addHeader(headers[i]);
			}
		}

		return request;
	}

	/**
	 * 
	 * @param offset
	 *            default value is 0.
	 * @param limit
	 *            default value is 30.
	 * @param keyword
	 *            the keyword to search the artists
	 * @return object of the request or <b><i>null</i></b> if keyword is
	 *         <b>null</b>.
	 */
	public static HttpGet getArtistsBySearchRequest(int offset, int limit,
			String keyword, Header[] headers) {
		if (offset < 0)
			offset = 0;
		if (limit <= 0)
			limit = 30;
		if (keyword == null)
			return null;
		List<BasicNameValuePair> valuePairs = new ArrayList<BasicNameValuePair>();
		valuePairs.add(new BasicNameValuePair("offset", offset + ""));
		valuePairs.add(new BasicNameValuePair("limit", limit + ""));
		valuePairs.add(new BasicNameValuePair("keyword", keyword));

		String url = URLEncodedUtils.format(valuePairs, "utf-8");
		url = Constants.API_MELON_SEARCH_INTEGRATION_ARTIST + url;

		HttpGet request = new HttpGet(url);
		request.addHeader("Accept", "application/json");
		if (headers != null) {
			for (int i = 0; i < headers.length; ++i) {
				request.addHeader(headers[i]);
			}
		}

		return request;
	}

	/**
	 * Creates the object of HTTP GET request for the song containing the
	 * keyword.
	 * 
	 * @param offset
	 *            default value is 0.
	 * @param limit
	 *            default value is 30.
	 * @param keyword
	 *            the keyword you will use to be a filter, can't be <b>null</b>.
	 * @return object of request or <b><i>null</i></b> if keyword is
	 *         <b>null</b>.
	 */
	public static HttpGet getSongsBySearchRequest(int offset, int limit,
			String keyword, Header[] headers) {
		if (keyword == null)
			return null;
		if (offset < 0)
			offset = 0;
		if (limit <= 0)
			limit = 30;

		List<BasicNameValuePair> valuePairs = new ArrayList<BasicNameValuePair>();
		valuePairs.add(new BasicNameValuePair("offset", offset + ""));
		valuePairs.add(new BasicNameValuePair("limit", limit + ""));
		valuePairs.add(new BasicNameValuePair("keyword", keyword));

		String url = URLEncodedUtils.format(valuePairs, "utf-8");
		url = Constants.API_MELON_SEARCH_INTEGRATION_SONG + url;

		HttpGet request = new HttpGet(url);
		request.addHeader("Accept", "application/json");
		if (headers != null) {
			for (int i = 0; i < headers.length; ++i) {
				request.addHeader(headers[i]);
			}
		}

		return request;
	}

	/**
	 * @param offset
	 *            default value is 0.
	 * @param limit
	 *            default value is 30.
	 * @param keyword
	 *            can't be <b>null</b>.
	 * @return
	 */
	public static HttpGet getAlbumsBySearchRequest(int offset, int limit,
			String keyword, Header[] headers) {
		if (keyword == null)
			return null;
		if (offset < 0)
			offset = 0;
		if (limit <= 0)
			offset = 30;

		List<BasicNameValuePair> valuePairs = new ArrayList<BasicNameValuePair>();
		valuePairs.add(new BasicNameValuePair("offset", offset + ""));
		valuePairs.add(new BasicNameValuePair("limit", limit + ""));
		valuePairs.add(new BasicNameValuePair("keyword", keyword));

		String url = URLEncodedUtils.format(valuePairs, "utf-8");
		url = Constants.API_MELON_SEARCH_INTEGRATION_SONG + url;

		HttpGet request = new HttpGet(url);
		request.addHeader("Accept", "application/json");
		if (headers != null) {
			for (int i = 0; i < headers.length; ++i) {
				request.addHeader(headers[i]);
			}
		}

		return new HttpGet();
	}

	/**
	 * Creates HTTP POST request with certain parameters based on user input.
	 * 
	 * @param email
	 *            can't be <b>null</b>
	 * @param webPassword
	 *            can't be <b>null</b>
	 * @param nickname
	 *            can't be <b>null</b>
	 * @param gender
	 * @param birthday
	 * @param msisdn
	 * @param provinceId
	 * @param quizId
	 *            can't be <b>null</b>
	 * @param quizAnswer
	 *            can't be <b>null</b>
	 * @param smsEvent
	 * @param newsMailing
	 * @return <b>null</b> if email or webPassword or nickname or quizId or
	 *         quizAnswer is null, otherwise returns the object of the request
	 */
	public static HttpPost getUserRegistrationRequest(String email,
			String webPassword, String nickname, String gender,
			String birthday, String msisdn, String provinceId, String quizId,
			String quizAnswer, String smsEvent, String newsMailing) {
		if (email == null || webPassword == null || nickname == null
				|| quizId == null || quizAnswer == null)
			return null;

		String sgender = gender, ssmsEvent = smsEvent, snewsMailing = newsMailing;
		if ((sgender != "M" && sgender != "F") || (sgender == null))
			sgender = "M";
		if (snewsMailing != "Y" && snewsMailing != "N")
			snewsMailing = "N";
		if (ssmsEvent != "Y" && ssmsEvent != "N")
			ssmsEvent = "N";

		List<BasicNameValuePair> valuePairs = new ArrayList<BasicNameValuePair>();
		valuePairs.add(new BasicNameValuePair("email", email));
		valuePairs.add(new BasicNameValuePair("webPassword", webPassword));
		valuePairs.add(new BasicNameValuePair("nickname", nickname));
		valuePairs.add(new BasicNameValuePair("gender", sgender));
		valuePairs.add(new BasicNameValuePair("birthday", birthday));
		valuePairs.add(new BasicNameValuePair("msisdn", msisdn));
		valuePairs.add(new BasicNameValuePair("provinceId", provinceId));
		valuePairs.add(new BasicNameValuePair("quizId", quizId));
		valuePairs.add(new BasicNameValuePair("quizAnswer", quizAnswer));
		valuePairs.add(new BasicNameValuePair("eventSmsYN", ssmsEvent));
		valuePairs.add(new BasicNameValuePair("newMailing", snewsMailing));

		HttpPost request = new HttpPost(Constants.API_MELON_USER_REGISTRATION);
		try {
			request.setHeader("Accept", "application/json");
			request.setEntity(new UrlEncodedFormEntity(valuePairs));
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			Log.i(TAG, e.getMessage());
		}

		return request;
	}

	/**
	 * 
	 * @param offset
	 * @param limit
	 * @param direction
	 *            either you want your list ascending ('asc') or descending
	 *            ('desc')
	 * @param artistId
	 *            the id of the artist you desired to look and can't be
	 *            <b>null</b>
	 * @return the instance of the request.
	 */
	public static HttpGet getAlbumsByArtistRequest(int offset, int limit,
			String direction, int artistId, Header[] headers) {
		if ((direction != "asc" && direction != "desc") || (direction == null))
			direction = "desc";
		if (limit <= 0)
			limit = 30;
		if (offset < 0)
			offset = 0;
		if (artistId <= 0)
			return null;
		List<BasicNameValuePair> valuePairs = new ArrayList<BasicNameValuePair>();
		valuePairs.add(new BasicNameValuePair("offset", offset + ""));
		valuePairs.add(new BasicNameValuePair("limit", limit + ""));
		valuePairs.add(new BasicNameValuePair("dir", direction));
		valuePairs.add(new BasicNameValuePair("artistId", artistId + ""));

		String url = URLEncodedUtils.format(valuePairs, "utf-8");
		url = Constants.API_MELON_ALBUM_BY_ARTIST + "?" + url;
		Log.i(TAG, url);

		HttpGet request = new HttpGet(url);
		request.addHeader("Accept", "application/json");
		if (headers != null) {
			for (int i = 0; i < headers.length; ++i) {
				request.addHeader(headers[i]);
			}
		}

		return request;
	}

	/**
	 * Creates the HTTP GET request of the album with certain genre.
	 * 
	 * @param offset
	 * @param limit
	 *            the limit of results you want to display. default is 10.
	 * @param direction
	 *            the sorting order, either 'asc' or 'desc'. if you fill this
	 *            with something else, it will turn to 'desc'.
	 * @param genreId
	 *            the id of the genre and it can't be <b>null</b> since you want
	 *            to get album by its genre.
	 * @return the instance of the request about album filtered by genre.
	 */
	public static HttpGet getAlbumsByGenreRequest(int offset, int limit,
			String direction, int genreId, Header[] headers) {
		if ((direction != "asc" && direction != "desc") || (direction == null))
			direction = "desc";
		if (limit <= 0)
			limit = 30;
		if (offset < 0)
			offset = 0;

		List<BasicNameValuePair> valuePairs = new ArrayList<BasicNameValuePair>();
		valuePairs.add(new BasicNameValuePair("offset", offset + ""));
		valuePairs.add(new BasicNameValuePair("limit", limit + ""));
		valuePairs.add(new BasicNameValuePair("dir", direction));
		if (genreId <= 0) {
			valuePairs.add(new BasicNameValuePair("genreId", "null"));
		} else {
			valuePairs.add(new BasicNameValuePair("genreId", genreId + ""));
		}

		String url = URLEncodedUtils.format(valuePairs, "utf-8");
		url = Constants.API_MELON_ALBUM_BY_GENRE + "?" + url;
		Log.i(TAG, url);

		HttpGet request = new HttpGet(url);
		request.addHeader("Accept", "application/json");
		if (headers != null) {
			for (int i = 0; i < headers.length; ++i) {
				request.addHeader(headers[i]);
			}
		}

		return request;
	}

	/**
	 * Creates HTTP GET request to get newest album
	 * 
	 * @param offset
	 *            anything as long it is a number otherwise it's going to be 0.
	 * @param limit
	 *            default is 10 but you can mention another number.
	 * @param direction
	 *            either 'asc' or 'desc' otherwise it is 'desc'.
	 * @param genreId
	 *            can be null or not. it's up to you.
	 * @return the instance of the request
	 */
	public static HttpGet getAlbumsByNewestRequest(int offset, int limit,
			String direction, int genreId, Header[] headers) {
		if (limit <= 0)
			limit = 30;
		if (offset < 0)
			offset = 0;

		List<BasicNameValuePair> valuePairs = new ArrayList<BasicNameValuePair>();
		valuePairs.add(new BasicNameValuePair("offset", offset + ""));
		valuePairs.add(new BasicNameValuePair("limit", limit + ""));
		valuePairs.add(new BasicNameValuePair("dir", direction));
		if (genreId <= 0) {
			valuePairs.add(new BasicNameValuePair("genreId", null));
		} else {
			valuePairs.add(new BasicNameValuePair("genreId", genreId + ""));
		}

		String url = URLEncodedUtils.format(valuePairs, "utf-8");
		url = Constants.API_MELON_ALBUM_NEW + url;

		Log.i(TAG, url);

		HttpGet request = new HttpGet(url);
		request.addHeader("Accept", "application/json");
		if (headers != null) {
			for (int i = 0; i < headers.length; ++i) {
				request.addHeader(headers[i]);
			}
		}

		return request;
	}

	/**
	 * Creates the request for the hottest album.
	 * 
	 * @param limit
	 *            can be <b>null</b>
	 * @param genreId
	 *            the id of the genre you want from the album, it may be
	 *            <b>null</b>
	 * @return the instance of the request
	 */
	public static HttpGet getAlbumByHottestRequest(int limit, int genreId,
			Header[] headers) {
		if (limit <= 0)
			limit = 30;

		List<BasicNameValuePair> valuePairs = new ArrayList<BasicNameValuePair>();
		valuePairs.add(new BasicNameValuePair("limit", limit + ""));
		if (genreId <= 0) {
			valuePairs.add(new BasicNameValuePair("genreId", "null"));
		} else {
			valuePairs.add(new BasicNameValuePair("genreId", genreId + ""));
		}

		String url = URLEncodedUtils.format(valuePairs, "utf-8");
		url = Constants.API_MELON_ALBUM_HOT + "?" + url;

		Log.i(TAG, url);

		HttpGet request = new HttpGet(url);
		request.addHeader("Accept", "application/json");
		if (headers != null) {
			for (int i = 0; i < headers.length; ++i) {
				request.addHeader(headers[i]);
			}
		}

		return request;
	}

	/**
	 * 
	 * @param albumId
	 *            the id of the desired album and it is <b>required!</b>
	 * @return
	 */
	public static HttpGet getAlbumByAlbumIdRequest(int albumId, Header[] headers) {
		if (albumId <= 0)
			return null;

		List<BasicNameValuePair> valuePairs = new ArrayList<BasicNameValuePair>();
		valuePairs.add(new BasicNameValuePair("albumId", albumId + ""));

		String url = URLEncodedUtils.format(valuePairs, "utf-8");
		url = Constants.API_MELON_ALBUM_BY_ALBUM_ID + url;

		Log.i(TAG, url);

		HttpGet request = new HttpGet(url);
		request.addHeader("Accept", "application/json");
		if (headers != null) {
			for (int i = 0; i < headers.length; ++i) {
				request.addHeader(headers[i]);
			}
		}

		return request;
	}

	/**
	 * 
	 * @param artistId
	 *            the id of the desired artist and it is <b>required!</b>
	 * @return
	 */
	public static HttpGet getArtistsByArtistIdRequest(int artistId,
			Header[] headers) {
		if (artistId <= 0)
			return null;
		List<BasicNameValuePair> valuePairs = new ArrayList<BasicNameValuePair>();
		valuePairs.add(new BasicNameValuePair("artistId", artistId + ""));

		String url = URLEncodedUtils.format(valuePairs, "utf-8");
		url = Constants.API_MELON_ARTIST_DETAILS + url;

		Log.i(TAG, url);

		HttpGet request = new HttpGet(url);
		request.addHeader("Accept", "application/json");
		if (headers != null) {
			for (int i = 0; i < headers.length; ++i) {
				request.addHeader(headers[i]);
			}
		}

		return request;
	}

	/**
	 * Creates Http Get request to obtain response artists list based on the
	 * hottest category. This API needs authentication first.
	 * 
	 * @param limit
	 * @return
	 */
	public static HttpGet getArtistsByHottestRequest(int limit, Header[] headers) {
		if (limit <= 0)
			limit = 30;
		List<BasicNameValuePair> valuePairs = new ArrayList<BasicNameValuePair>();
		valuePairs.add(new BasicNameValuePair("limit", limit + ""));

		String url = URLEncodedUtils.format(valuePairs, "utf-8");
		url = Constants.API_MELON_ARTIST_HOT + "?" + url;

		Log.i(TAG, url);

		HttpGet request = new HttpGet(url);
		request.addHeader("Accept", "application/json");
		if (headers != null) {
			for (int i = 0; i < headers.length; ++i) {
				request.addHeader(headers[i]);
			}
		}

		return request;
	}

	/**
	 * Creates Http Get request to get songs based on the album. This request
	 * needs authentication first.
	 * 
	 * @param offset
	 * @param limit
	 *            the number of song you want to achieve.
	 * @param albumId
	 *            <b><i>Required</i></b>
	 * @return
	 */
	public static HttpGet getSongsByAlbumRequest(int offset, int limit,
			int albumId, Header[] headers) {
		if (albumId <= 0)
			return null;
		if (offset < 0)
			offset = 0;
		if (limit <= 0)
			limit = 30;
		List<BasicNameValuePair> valuePairs = new ArrayList<BasicNameValuePair>();
		valuePairs.add(new BasicNameValuePair("offset", offset + ""));
		valuePairs.add(new BasicNameValuePair("limit", limit + ""));
		valuePairs.add(new BasicNameValuePair("albumId", albumId + ""));

		String url = URLEncodedUtils.format(valuePairs, "utf-8");
		url = Constants.API_MELON_SONG_BY_ALBUM + "?" + url;

		Log.i(TAG, url);

		HttpGet request = new HttpGet(url);
		request.addHeader("Accept", "application/json");
		if (headers != null) {
			for (int i = 0; i < headers.length; ++i) {
				request.addHeader(headers[i]);
			}
		}

		return request;
	}

	/**
	 * Creates Http Get request to get songs based on the artists. This request
	 * needs an authentication first.
	 * 
	 * @param offset
	 * @param limit
	 * @param artistId
	 *            <b><i>Required</i></b>
	 * @return
	 */
	public static HttpGet getSongsByArtistRequest(int offset, int limit,
			int artistId, Header[] headers) {
		if (artistId <= 0)
			return null;
		if (limit <= 0)
			limit = 30;
		if (offset < 0)
			offset = 0;

		List<BasicNameValuePair> valuePairs = new ArrayList<BasicNameValuePair>();
		valuePairs.add(new BasicNameValuePair("offset", offset + ""));
		valuePairs.add(new BasicNameValuePair("limit", limit + ""));
		valuePairs.add(new BasicNameValuePair("artistId", artistId + ""));

		String url = URLEncodedUtils.format(valuePairs, "utf-8");
		url = Constants.API_MELON_SONG_BY_ARTIST + "?" + url;

		Log.i(TAG, url);

		HttpGet request = new HttpGet(url);
		request.addHeader("Accept", "application/json");
		if (headers != null) {
			for (int i = 0; i < headers.length; ++i) {
				request.addHeader(headers[i]);
			}
		}

		return request;
	}

	/**
	 * 
	 * @param offset
	 * @param limit
	 * @param genreId
	 * @return
	 */
	public static HttpGet getSongsByGenreRequest(int offset, int limit,
			int genreId, Header[] headers) {
		if (limit <= 0)
			limit = 30;
		if (offset < 0)
			offset = 0;

		List<BasicNameValuePair> valuePairs = new ArrayList<BasicNameValuePair>();
		valuePairs.add(new BasicNameValuePair("offset", offset + ""));
		valuePairs.add(new BasicNameValuePair("limit", limit + ""));
		if (genreId <= 0) {
			valuePairs.add(new BasicNameValuePair("genreId", "null"));
		} else {
			valuePairs.add(new BasicNameValuePair("genreId", genreId + ""));
		}

		String url = URLEncodedUtils.format(valuePairs, "utf-8");
		url = Constants.API_MELON_SONG_BY_GENRE + "?" + url;

		Log.i(TAG, url);

		HttpGet request = new HttpGet(url);
		request.addHeader("Accept", "application/json");
		if (headers != null) {
			for (int i = 0; i < headers.length; ++i) {
				request.addHeader(headers[i]);
			}
		}

		return request;
	}

	/**
	 * Creates Http Get request to get the newest songs. This request doesn't
	 * need authentication.
	 * 
	 * @param offset
	 * @param limit
	 * @param direction
	 * @param genreId
	 * @return
	 */
	public static HttpGet getSongsByNewestRequest(int offset, int limit,
			String direction, int genreId, Header[] headers) {
		if (offset < 0)
			offset = 0;
		if (limit <= 0)
			limit = 30;
		if ((direction != "asc" && direction != "desc") || (direction == null))
			direction = "desc";

		List<BasicNameValuePair> valuePairs = new ArrayList<BasicNameValuePair>();
		valuePairs.add(new BasicNameValuePair("offset", offset + ""));
		valuePairs.add(new BasicNameValuePair("limit", limit + ""));
		valuePairs.add(new BasicNameValuePair("dir", direction));
		if (genreId <= 0) {
			valuePairs.add(new BasicNameValuePair("genreId", "null"));
		} else {
			valuePairs.add(new BasicNameValuePair("genreId", genreId + ""));
		}

		String url = URLEncodedUtils.format(valuePairs, "utf-8");
		url = Constants.API_MELON_SONG_BY_NEW + "?" + url;

		Log.i(TAG, url);

		HttpGet request = new HttpGet(url);
		request.addHeader("Accept", "application/json");
		if (headers != null) {
			for (int i = 0; i < headers.length; ++i) {
				request.addHeader(headers[i]);
			}
		}

		return request;
	}

	/**
	 * 
	 * @return
	 */
	public static HttpGet getGenreByCategoriesRequest(Header[] headers) {
		HttpGet request = new HttpGet(Constants.API_MELON_GENRE_CATEGORIES);
		request.addHeader("Accept", "application/json");
		if (headers != null) {
			for (int i = 0; i < headers.length; ++i) {
				request.addHeader(headers[i]);
			}
		}

		return request;
	}

	/**
	 * Creates Http Get request for genre by category ID. This request needs
	 * authentication for Melon API usages.
	 * 
	 * @param categoryId
	 *            the id of category you need to find the genre and can't be
	 *            <b>null</b>
	 * @param headers
	 *            the header you may need to attach
	 * @return <b><i>null</i></b> if categoryId is <b>null</b>
	 */
	public static HttpGet getGenreByCategoryIdRequest(int categoryId,
			Header[] headers) {
		if (categoryId <= 0)
			return null;
		List<BasicNameValuePair> valuePairs = new ArrayList<BasicNameValuePair>();
		valuePairs.add(new BasicNameValuePair("categoryId", categoryId + ""));

		String url = URLEncodedUtils.format(valuePairs, "utf-8");
		url = Constants.API_MELON_GENRE_CATEGORIES_BY_ID + categoryId + ""
				+ "/genres/";

		Log.i(TAG, url);

		HttpGet request = new HttpGet(url);
		request.addHeader("Accept", "application/json");
		if (headers != null) {
			for (int i = 0; i < headers.length; ++i) {
				request.addHeader(headers[i]);
			}
		}

		return request;
	}

	/**
	 * This request needs authentication first.
	 * 
	 * @return the HTTP GET request for genre by allowing all categories.
	 */
	public static HttpGet getGenreByAllCategoryRequest(Header[] headers) {
		HttpGet request = new HttpGet(Constants.API_MELON_GENRE_CATEGORIES_ALL);
		request.addHeader("Accept", "application/json");
		if (headers != null) {
			for (int i = 0; i < headers.length; ++i) {
				request.addHeader(headers[i]);
			}
		}

		return request;
	}
}
